<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\UserVideoComment;
use app\common\model\BusinessVideo as VideoModel;
use app\common\model\Product;
use app\common\model\UserVideoHit;
use app\common\model\VideoCate;
use app\common\model\VideoTag;
use app\common\tool\Result;
use Qiniu\Auth;
use think\Response;

class BusinessVideo extends Base
{

    /**
     * 视频上传/修改
     */
    public function store() :Response
    {
        try{
            $param = $this->request->post();
            $this->validate($param, 'BusinessVideo');
            $result = VideoModel::_save($this->request->param(), $this->user);

            return Result::fastReturn($result);
        }catch (\Exception $e){
            return Result::fail($e->getMessage());
        }

    }

    /**
     * 获取token
     * @return Response
     */
    public function token() :Response
    {
        try{
            $config = config('qiniu');
            // 构建鉴权对象
            $auth = new Auth($config['accessKey'], $config['secretKey']);
            $token = $auth->uploadToken($config['bucket']);

            $result = ['code' => 1, 'msg' => 'success', 'data' => ['token'=>$token]];

            return Result::fastReturn($result);
        }catch (\Exception $e){
            return Result::fail($e->getMessage());
        }

    }

    /**
     * 视频列表
     */
    public function index() :Response
    {
        $map['is_del'] = 0;
        $map['business_id'] = $this->business->id;

        $param = $this->request->param();
        if(!empty($param['cate_id'])) $map['cate_id'] = $param['cate_id'];
        $like = [];
        if(!empty($param['keyword'])){
            $like = [
                'field' => 'tag|introduce',
                'keyword' => $param['keyword']
            ];
        }

        $field = 'id,cover,introduce,link,tag,total_hit,total_comment,total_view,total_share,product_id,cate_id';
        $list = VideoModel::getPageList($map, $field, $this->getPage(), $this->getLimit(), false, ['cate'], [], [], $like);

        return Result::fastReturn($list);
    }

    /**
     * 视频详情
     */
    public function info() :Response
    {
        $id = $this->request->param('id',0);

        if(!$info = VideoModel::with(['comment','cate'])->where(['id'=>$id,'is_del'=>0,'business_id'=>$this->business->id])
            ->field('id,title,introduce,link,tag,total_hit,total_comment,total_seconds,total_view,total_share,product_id')->find()){
            return Result::fail('视频不存在或已被删除');
        }

        return Result::fastReturn(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     * 链接商品
     */
    public function product() :Response
    {
        $id = $this->request->param('product_id',0);

        if(!$info = Product::where(['id'=>$id,'is_del'=>0,'business_id'=>$this->business->id])->field('id,title,describe,icon,price')->find()){
            return Result::fail('商品不存在或已被删除');
        }

        return Result::fastReturn(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     * 视频删除
     */
    public function del() :Response
    {
        $ids = $this->request->param('ids',0);

        $result = VideoModel::whereIn('id', $ids)->save(['is_del'=>1]);
        if($result){
            return Result::success('删除成功');
        }
        return Result::fail('删除失败');
    }

    /**
     * 点赞列表
     */
    public function hitList() :Response
    {
        $map['is_del'] = 0;
        $map['video_id'] = $this->request->get('video_id', 0);
        $map['business_id'] = $this->business->id;

        $listRows = [
            'page' => $this->getPage(),
            'list_rows' => $this->getLimit()
        ];
        $list = UserVideoHit::with(['user'])->where($map)->field('id,uid,video_id,create_time,business_id')->order('id', 'desc')->paginate($listRows);

        $result = ['code'=>1, 'msg'=>'success', 'data'=>$list];
        return Result::fastReturn($result);
    }

    /**
     * 评论列表
     */
    public function commentList() :Response
    {
        $map['is_del'] = 0;
        $map['business_id'] = $this->business->id;
        if($video_id = $this->request->get('video_id', 0)){
            $map['video_id'] = $video_id;
        }

        $listRows = [
            'page' => $this->getPage(),
            'list_rows' => $this->getLimit()
        ];
        $list = UserVideoComment::with(['user', 'video'])->where($map)->field('id,uid,video_id,create_time,business_id,comment')->order('id', 'desc')->paginate($listRows);

        $result = ['code'=>1, 'msg'=>'success', 'data'=>$list];
        return Result::fastReturn($result);
    }

    /**
     * 视频评论删除
     */
    public function delComment() :Response
    {
        $ids = $this->request->param('ids','');
        $business_id = $this->business->id;

        return Result::fastReturn(UserVideoComment::del($ids, $business_id));
    }

    /**
     * 视频评论删除订单支付
     */
    public function commentDelOrder() :Response
    {
        try {
            $param = $this->request->post();
            $this->validate($param, 'OrderVideoCommentDel');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }

        return Result::fastReturn(UserVideoComment::payment($param, $this->user));
    }

    /**
     * 添加标签、话题
     * @return Response
     */
    /*public function addTag() :Response
    {
        try {
            if(!$tag = $this->request->get('tag')){
                return Result::fail('内容不能为空');
            }

            if(!VideoTag::where('tag', $tag)->find()){
                VideoTag::create([
                    'tag' => $tag
                ]);
            }
            return Result::success('操作成功');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }*/

    /**
     * 标签、话题列表
     * @return Response
     */
    public function tagList() :Response
    {
        try {
            $tags = VideoTag::where('is_del', 0)->field('id,tag')->order('cites', 'desc')->limit(15)->select();

            $result = ['code'=>1, 'msg'=>'success', 'data'=>$tags];
            return Result::fastReturn($result);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }


}
